package com.gdth.business.dao.impl;

import java.util.List;

import com.gdth.bank.BankApplication;
import com.gdth.business.dao.IBaseDao;
import com.gdth.database.bean.DaoSession;

import org.greenrobot.greendao.AbstractDao;


/** 
 * @author  Arvin: 
 * @version 2016年5月26日 上午10:29:27 
 * 
 */
public class BaseDaoImpl<T> implements IBaseDao<T>{

	protected Class<T> entityClass;  
	protected String className; 
	
	protected AbstractDao<T, Long> dao;
	
	public BaseDaoImpl(){  
//		entityClass=(Class<T>)((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];  
//		
//		className=entityClass.getName();  
	}

	public DaoSession getDaoSession(){
		return BankApplication.getDaoSession();
	}
	
	public void setAbstractDao(AbstractDao<T, Long> dao){
		this.dao = dao;
	}

	@Override
	public T load(long id) {
		// TODO Auto-generated method stub
		return this.dao.load(id);
	}
	
	@Override
	public List<T> loadAll() {
		// TODO Auto-generated method stub
		return this.dao.loadAll();
	}

	@Override
	public List<T> query(String where, String... params) {
		// TODO Auto-generated method stub
		return this.dao.queryRaw(where, params);
	}

	@Override
	public long save(T t) {
		// TODO Auto-generated method stub
		return this.dao.insertOrReplace(t);
	}

	@Override
	public void saveLists(final List<T> list) {
		// TODO Auto-generated method stub
		if(list == null || list.isEmpty()){
			return;
		}
		
		getDaoSession().runInTx(new Runnable() {
			@Override
			public void run() {
				for(int  i= 0; i < list.size(); i++){
					T t = list.get(i);
					save(t);
				}
			}
		});
	}

	@Override
	public void delete(long id) {
		// TODO Auto-generated method stub
		this.dao.deleteByKey(id);
	}
	
	@Override
	public void delete(T t) {
		// TODO Auto-generated method stub
		this.dao.delete(t);
	}
	
	@Override
	public void deleteAll() {
		// TODO Auto-generated method stub
		this.dao.deleteAll();
	}
}
